package com.xm.spider;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.Future;

/**
 * 翻译结果转txt任务
 */
@Slf4j
public class Trans2TxtTask implements Runnable{
    private final int page;
    private final String filePath;//D:\data\新建文件夹\output.txt
    private final String folder;
    private final List<Future<String>> futures;

    public Trans2TxtTask(int page, String filePath, String folder, List<Future<String>> futures) {
        this.page = page;
        this.filePath = filePath;
        this.folder = folder;
        this.futures = futures;
    }

    @Override
    public void run() {
        try {
            //等待并发执行结果
            SeleniumTrans.waitFutures(futures);
            //循环resultMap，获取翻译结果
            String content = SeleniumTrans.getContent(page);
            log.info("爬虫正在将翻译结果写入txt文件{}", page);//TEST
            //将整章节的翻译结果存入txt
            SeleniumTrans.transToTxt(folder, filePath, content);//TEST
        } finally {
            SeleniumTrans.resultList.remove(page);//本次翻译完成 及时清理内存
        }
    }
}
